import {
  Controller,
  Get,
  Post,
  Body,
  Req,
  UseGuards,
  Put,
} from '@nestjs/common';
import { Public } from 'src/common/guard/public.decorator';
import { ApiUserService } from './user.service';
import { RequestWithUser } from 'src/type';
import { JwtAuthGuard } from 'src/common/guard/jwt-auth.guard';
import { CreateUserDto } from 'src/user/dto/create-user.dto';
import { UserEntity } from 'src/user/entities/user.entity';
import { updateDto } from './dto/update.dto';

@UseGuards(JwtAuthGuard)
@Controller('/api/user')
export class ApiUserController {
  constructor(private readonly apiUserService: ApiUserService) {}

  // 登录
  @Public()
  @Post('login')
  login(@Body() payload: CreateUserDto) {
    return this.apiUserService.login(payload);
  }
  // 登录
  @Public()
  @Post('register')
  register(@Body() payload: CreateUserDto) {
    return this.apiUserService.register(payload);
  }

  // 获取用户信息
  @Get('getUserInfo')
  getUserInfo(@Req() request: RequestWithUser) {
    return this.apiUserService.getUser(request.user);
  }

  @Put('')
  updateUserInfo(@Req() req: { user: UserEntity }, @Body() payload: updateDto) {
    const user = req.user;
    this.apiUserService.updateUserInfo(user, payload);
  }
}
